﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Extensil.Common.CategoryHandler
{
    public class PluginHandlers :ConfigurationElementCollection
    {
        public PluginHandler this[int index]
        {
            get
            {
                return base.BaseGet(index) as PluginHandler;
            }
            set
            {
                if(base.Count > index && base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
        public PluginHandler this[string key]
        {
            get
            {
                return base.BaseGet(key) as PluginHandler;
            }
        }
        public PluginHandler GetPluginHandlerWithPlugName(string plugName)
        {
            foreach(PluginHandler ph in this)
            {
                if(ph.PlugName.ToLower() == plugName.ToLower())
                    return ph;
            }
            return null;
        }
        public override bool IsReadOnly()
        {
            return false;
        }

        internal void Add(PluginHandler cat)
        {
            this.BaseAdd(cat);
        }
        internal void Remove(PluginHandler cat)
        {
            this.BaseRemove(cat.Name);
        }
        internal int IndexOf(PluginHandler cat)
        {
            return base.BaseIndexOf(cat);
        }
        
        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return base.CreateNewElement(elementName);
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new PluginHandler();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((PluginHandler)element).Name;
        }

    }
    
}
